<?php

namespace Rblog\Module\Admin\Model;

class User extends \Rexzy\App\Model {

    protected $_table = 'users';

    /**
     *  查询用户ID函数
     * 
     * @param string $name 查询的用户名
     * @return int 返回查询用户的用户ID
     */
    public function userid($name) {
        $where = array( 'name' => $name );
        $result = $this->select('userid', $where);
        if ( ! $result ) {
            return false;
        }
        return $result['userid'];
    }

    /**
     * 新增管理用户的信息写入
     * 
     * @param type $name 用户名
     * @param type $passwd 用户密码
     * @return type 用户ID
     */
    public function add( $name, $passwd, $screenName, $mail, $group ) {
        $data = Array( 
            'name' => $name,
            'password' => $passwd,
            'screenName' => $screenName,
            'email' => $mail,
            'usergroup' => $group
            );
        
        $result = $this->insert( $data );
        
        return true;
    }
    
    /**
     * 查询所有用户的列表
     * 
     * @param array() $condition 查询的排除条件
     * @param int $start 开始查询的位置
     * @param int $length 查询的结果量
     * @return array 查询结果
     */
    public function getList( $condition, $start = 0, $length = 100000 ) {
        $filed = 'userid, name, screenName, email, usergroup';
        
        if ( ( ! empty( $condition ) ) && ( isset( $condition['seach'] ) ) ) {
            $where = "where name like '%" . $condition['seach'] . "%' or screenName like '%" . $condition['seach'] . "%' or email like '%" . $condition['seach'] . "%'";
            $where .= "group by userid order by userid desc"; 
        } else {
            $where = " group by userid order by userid desc";
        }
        
        $limit = $start . ', ' . $length; 
        $result = $this->selectAll( $filed, $where, $limit );
        $groupMesg = array( 
            '关注者' => 'subscriber',
            '贡献者' => 'contributor',
            '编辑' => 'editor',
            '管理员' => 'administrator',
        );
        foreach( $result as $key => $val ) {
            $result[$key]['groupname'] = array_keys( $groupMesg, $val['usergroup'] )[0];
        }
        return $result;
    }
    
    /**
     * 删除用户
     * 
     * @param mixed $userid 可以是直接删除一组用户，也可以是删除单个用户
     * @return boolean 删除结果
     */
    public function delUser( $userid ) {
        if ( is_array( $userid ) ) {
            $wherestr = array();
            foreach ( $userid as $key => $value ) {
                $wherestr[] = 'userid = ' . trim( $value );
            }
            $where = implode( ' or ', $wherestr );
        } else {
            $where = 'userid = ' . $userid;
        }
        
        $result = $this->delete( $where );
        return $result;
    }
    
    /** 
     * 查询个人用户的信息
     * 
     * @param int $userid 用户id
     * @return array 返回用户的信息
     */
    public function get( $userid ) {
        $where = 'where userid = ' . $userid;
        $filed = '*';
        
        $result = $this->select( $filed, $where );
        return $result;
    }
}
